﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraLayout.Utils;
using DevExpress.XtraLayout;
using DevExpress.XtraEditors;

namespace CIIS.Common
{
    public partial class Address : BaseUserControl
    {
        LayoutControlItem[] layouts = null;

        public Address()
        {
            InitializeComponent();

            layouts  = new LayoutControlItem[] { layoutControlItemS, layoutControlItemShi, layoutControlItemQu };

        }

        public void InitControl(bool[] visibleFlags, string[] captions)
        {
            if (visibleFlags == null || captions == null || visibleFlags.Length != 3 || captions.Length != 3)
            {
                return;
            }

            DevControlUtils.Instance.InitLookUpEdit(lookUpEditS, dao.GetProvinceDcp());

            for (int i = 0; i < visibleFlags.Length; i++)
            {
                layouts[i].Visibility = visibleFlags[i] ? LayoutVisibility.Always : LayoutVisibility.Never;
                layouts[i].TextVisible = !string.IsNullOrWhiteSpace(captions[i]);
                layouts[i].Text = captions[i];
            }

            lookUpEditS.EditValueChanged += new EventHandler(lookUpEditS_EditValueChanged);
            lookUpEditShi.EditValueChanged += new EventHandler(lookUpEditShi_EditValueChanged);
            lookUpEditQu.EditValueChanged += new EventHandler(lookUpEditQu_EditValueChanged);
        }

        void lookUpEditQu_EditValueChanged(object sender, EventArgs e)
        {
        }

        void lookUpEditShi_EditValueChanged(object sender, EventArgs e)
        {
            lookUpEditQu.Enabled = true;
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditQu, dao.GetCountyDcp(lookUpEditShi.EditValue.ToString()));
        }

        void lookUpEditS_EditValueChanged(object sender, EventArgs e)
        {
            lookUpEditShi.Enabled = true;
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditShi, dao.GetCityDcp(lookUpEditS.EditValue.ToString()));
        }

        public void SetEditValue(string[] addresses)
        {
            if (addresses == null || addresses.Length > layouts.Length)
            {
                return;
            }
            string[] addressIds = DataUtils.Instance.GetAddressCode(addresses);

            for (int i = 0; i < addresses.Length; i++)
            {
                LookUpEdit lue = layouts[i].Control as LookUpEdit;
                if (lue != null &&  !string.IsNullOrWhiteSpace(addressIds[i]))
                {
                    lue.EditValue = addressIds[i];
                }
            }
        }

        /// <summary>
        /// 加空格以便分隔
        /// </summary>
        /// <returns></returns>
        public string GetAddress()
        {
            return string.Format("{0} {1} {2}",lookUpEditS.EditValue != null ? lookUpEditS.Text : string.Empty,
                lookUpEditShi.EditValue != null ? lookUpEditShi.Text : string.Empty, 
                lookUpEditQu.EditValue != null ?  lookUpEditQu.Text : string.Empty);
        }
    }
}
